import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LassoCV,Ridge
from sklearn.linear_model import LinearRegression

poly_min=2 # lowest degree of polynomial we want
poly_max=8 # highest degree of polynomial we want



proc_data= pd.read_pickle("processed_data.pkl")
proc_data=proc_data.drop([7,8],axis=1) # for now dropping convergence, not sure what we will do with that ?
print(proc_data)


labels=proc_data[[2]] ##impdeance
proc_features=proc_data.drop([2],axis=1) # processed features

## randomly splitting the data in 25 % for testing and 75 % for training, we have around 3000 examples
train_features, test_features, train_labels, test_labels = train_test_split(proc_features,labels , test_size = 0.15, random_state = 50)


# have to turn everything into numpy arrays
train_features=np.array(train_features)
train_labels=np.array(train_labels)
test_features=np.array(test_features)
test_labels=np.array(test_labels)
test_score=[]
testpreds=[]
RSME=[]
test_labels=test_labels.reshape(len(test_labels),)
rf=RandomForestRegressor(n_estimators=1000,random_state=50,criterion='mse')
rf.fit(train_features,train_labels)
test_pred=np.array(rf.predict(test_features))
#RSME=np.sqrt(np.sum(np.square(test_pred-test_labels)))
#test_score=rf.score(test_features,test_labels)
#print(test_pred)
errors=test_pred-test_labels
MPE=np.mean(abs(test_pred-test_labels))# get the mean prediction error for the impedance
print("MPE for Random forest is " + str(MPE))


# model=make_pipeline(PolynomialFeatures(3,interaction_only=False),
#                     LassoCV(eps=1e-6,n_alphas=5,max_iter=5000,normalize=True,cv=5,tol=1e-2))
# print(train_features)
# model.fit(train_features,train_labels)
# test_pred=model.predict(test_features)
# MPE=np.mean(abs(test_pred-test_labels))# get the mean prediction error for the impedance
# print("MPE for LassoCV is " + str(MPE))

for x in range(1,10): # try different polynomial features
    model=make_pipeline(PolynomialFeatures(x,interaction_only=False),
                        RandomForestRegressor(n_estimators=300,random_state=20,criterion='mse'))
    model.fit(train_features,train_labels)
    test_pred=model.predict(test_features)
    MPE=np.mean(abs(test_pred-test_labels))# get the mean prediction error for the impedance
    print("MPE for RFR is " + str(MPE)+" with polynomial= " + str(x))


